﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PTExpress.Models;
using System.Web.Routing;
using DTO;

namespace PTExpress.Controllers
{
    public class LoginController : AbstractController
    {

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Login model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (LoginService.ValidateUser(model.UserName, model.Password))
                {
                    DAO.NhanVienDAO nvDAO = new DAO.NhanVienDAO();
                    DTO.NhanVienDTO nv = nvDAO.getByTenDangNhap(model.UserName);

                    Session.Add(NhanVienDTO.SSNHANVIEN, nv);
                    FormsService.SignIn(model.UserName, false);

                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        return RedirectToAction("Index", "QLDatVe");
                    }
                    return Redirect(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            return View(model);
        }

        public ActionResult Logout()
        {
            FormsService.SignOut();
            Session.Remove(NhanVienDTO.SSNHANVIEN);
            return RedirectToAction("Index", "Login");
        }

    }
}
